<?php
defined('IN_LK') or die('Access Denied');
class form
{
	/**
	 * select 下拉框
	 * @param array      $options    array(0=>选项0,1=>选项1)
	 * @param string     $ext        select属性
	 * @param int/string $dv         默认值,形式可以是0或0,1
	 * @param string     $ext_option 可增加一项或几项option
	 * @param array      $attrs      选项的属性设置 array(选项key=>属性)
	 * @return string    $select     返回的select表单
	 */
	public static function select($options, $ext = '', $dv = 0, $ext_option = '', $attrs = array())
	{
		if(!is_array($options) || !$options) return false;
		$select = "<select $ext>\n$ext_option";
		$dv = isset($dv) && $dv ? explode(',', $dv) : array();
		foreach($options as $key => $option)
		{
			$check = in_array($key, $dv) ? 'selected="selected"' : '';
			$attr  = array_key_exists($key, $attrs) ? ' ' . $attrs[$key] : '';
			$select .= "<option value='$key' $check$attr>$option</option>\n";
		}
		$select .= "</select>\n";
		return $select;
	}
	
	/**
	 * 时间控件
	 * @param string $name 表单控件名
	 * @param string $value 值
	 * @param boolean $i_time 显示时间
	 * @param boolean $load  是否加载css及js
	 * @return string
	 */
	public static function input_date($name, $value = '', $is_time = true, $load = true)
	{
		$id = preg_match('/\[(.*)\]/', $name, $m) ? $m[1] : $name;
		if($is_time)
		{
			$size = 21;
			$format = '%Y-%m-%d %H:%M:%S';
			$showtime = 24;
		}
		else
		{
			$size = 10;
			$format = '%Y-%m-%d';
			$showtime = 'false';
		}
		$str = '';
		if($load)
		{
			$str .= '<link rel="stylesheet" type="text/css" href="' . JS_PATH . 'calendar/jscal2.css"/>
			<link rel="stylesheet" type="text/css" href="' . JS_PATH . 'calendar/border-radius.css"/>
			<link rel="stylesheet" type="text/css" href="' . JS_PATH . 'calendar/win2k.css"/>
			<script type="text/javascript" src="' . JS_PATH . 'calendar/calendar.js"></script>
			<script type="text/javascript" src="' . JS_PATH . 'calendar/lang/zh-cn.js"></script>';
		}
		$str .= '<input type="text" class="input_date" name="' . $name . '" id="' . $id . '" value="' . $value . '" size="' . $size .'" readonly="readonly" />';
		$str .= '<script type="text/javascript">Calendar.setup({weekNumbers: "true", inputField : "' . $id . '", trigger : "' . $id . '", dateFormat: "' . $format . '", showTime: ' . $showtime . ', onSelect:function() {this.hide();}});</script>';
		return $str;
	}
	
	
	/**
	 * 编辑器
	 * @param string $name    name值
	 * @param string $toolbar full/basic模式
	 * @param string $value   值
	 * @param boolean $load 是否强制加载 
	 */
	public static function editor($name, $toolbar = 'basic', $value = '', $load = true)
	{
		$editor = '';
		$id = preg_match('/\[(.*)\]/', $name, $m) ? $m[1] : $name;
		$items = $toolbar == 'basic' ? "items:['fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'link']" : '';
		if($load)
		{
			$editor .= "<script type=\"text/javascript\" src=\"" . JS_PATH . "kindeditor/kindeditor-min.js\"></script>";
		}
		$editor .= "<script type='text/javascript'>KE.show({id : '$id',resizeMode : 1,allowUpload : false,{$items}});</script>";
		$editor .= "<textarea id='$id' name='$name' style='width:500px;height:250px;display:inline-block;visibility:hidden;'>$value</textarea>";
		return $editor;
	}
	
   /**
    * checkbox
	* @param $name 
	*/
	public static function checkbox($name)
	{
		//TODO 未做
	}
	
}